#include "device.h"


// 创建设备管理节点
manager_t *createDoublyCircularLinkedList_manager()
{
    manager_t *Pmanager = calloc(1, sizeof(manager_t));
    if (Pmanager == NULL)
    {
        perror("Alloc Space failed!\n");
        return NULL;
    }
    Pmanager->num = 0;
    Pmanager->head = NULL;
    Pmanager->tail = NULL;
    return Pmanager;
}

// 创建节点(一本书相当于一个节点)
device_t *createDoubleCircularLinkedList_Node(int device_id,char device_name[50],char device_type[10],bool device_state)
{
    device_t *xnew = calloc(1, sizeof(device_t));
    if (xnew == NULL)
    {
        perror("Alloc Space failed!\n");
        return NULL;
    }
    xnew->device_id=device_id;
    strcpy(xnew->device_name, device_name);
    strcpy(xnew->device_type, device_type);
    xnew->device_state=device_state;
    xnew->prev = NULL;
    xnew->next = NULL;
    return xnew;
}

// 插入节点(设备)
// void DoubleCircularLinkedList_Push(manager_t *Pmanager, int device_id,char *device_name,char *device_type,bool device_state)
// {
//     device_t *xnew = createDoubleCircularLinkedList_Node( device_id, device_name, device_type, device_state);
//     if (Pmanager->head == NULL)
//     {
//         Pmanager->head = xnew;
//         Pmanager->tail = xnew;
//     }
//     else
//     {
//         xnew->prev = Pmanager->tail;
//         Pmanager->tail->next = xnew;
//         Pmanager->tail = xnew; // 尾结点更新
//     }
//     Pmanager->num++;
// }

//(已修改)
bool DoubleCircularLinkedList_Push(manager_t *Pmanager, int device_id, char *device_name, char *device_type, bool device_state) 
{
    // 参数检查
    if (!Pmanager || !device_name || !device_type) {
        printf("Error: Invalid parameters!\n");
        return false;
    }

    device_t *xnew = createDoubleCircularLinkedList_Node(device_id, device_name, device_type, device_state);
    if (!xnew) {
        printf("Error: Failed to create node!\n");
        return false;
    }

    if (Pmanager->head == NULL) {
        Pmanager->head = Pmanager->tail = xnew;
        xnew->next = xnew->prev = xnew; // 环形自引用
    } else {
        xnew->prev = Pmanager->tail;
        xnew->next = Pmanager->head;    // 闭环
        Pmanager->tail->next = xnew;
        Pmanager->head->prev = xnew;    
        Pmanager->tail = xnew;
    }

    Pmanager->num++;
    return true;
}

// 删除节点(设备)
bool DoubleCircularLinkedList_Delete(manager_t *Pmanager, int find_device_id)
{
    device_t *p = Pmanager->head;
    // 书库里面没书的情况下
    if (Pmanager->head == NULL)
    {
        return false;
    }

    while (p)
    {
        if (p->device_id==find_device_id)
        {
            break;
        }
        p = p->next;
    }

    // 找了一圈没找到设备
    while (p == NULL)
    {
        return false;
    }

    // 找到设备的情况下
    if (p == Pmanager->head)
    {
        Pmanager->head = p->next;
        p->next = NULL;
        Pmanager->head->prev = NULL;
        free(p);
    }
    else if (p == Pmanager->tail)
    {
        Pmanager->tail = p->prev;
        Pmanager->tail->next = NULL;
        p->prev = NULL;
        free(p);
    }
    else
    {
        p->prev->next = p->next;
        p->next->prev = p->prev;
        p->prev = NULL;
        p->next = NULL;
        free(p);
    }
    Pmanager->num--;
    return true;
}

// 打印所有设备信息
// bool DoubleCircularLinkedList_Print(manager_t *Pmanager)
// {
    
//     device_t *p = Pmanager->head;
//     if (Pmanager->head == NULL)
//     {
//         printf("系统还没录入任何设备哦！\n");
//         return false;
//     }

//     while (p)
//     {
//         printf("device_ID:%d\n", p->device_id);
//         printf("device_name:%s\n", p->device_name);
//         printf("device_type:%s\n", p->device_type);
//         printf("device_state:%d\n", p->device_state);
//         printf("-----------------------------\n");
//         p = p->next;
//     }
//     printf("the number of current device:%d\n", Pmanager->num);
//     return true;
// }

//(已修改)
bool DoubleCircularLinkedList_Print(manager_t *Pmanager) 
{
    if (!Pmanager || !Pmanager->head) {
        printf("系统还没录入任何设备哦！\n");
        return false;
    }

    device_t *p = Pmanager->head;
    do {
        printf("device_ID:%d\n", p->device_id);
        printf("device_name:%s\n", p->device_name);
        printf("device_type:%s\n", p->device_type);
        printf("device_state:%d\n", p->device_state);
        printf("-----------------------------\n");
        p = p->next;
    } while (p != Pmanager->head);  

    printf("the number of current device:%d\n", Pmanager->num);
    return true;
}

// 打印当前设备信息
void DoubleCircularLinkedList_CurrentPrint(device_t *current)
{
    printf("设备ID：%d\n", current->device_id);
    printf("设备名：%s\n", current->device_name);
    printf("设备类型：%s\n", current->device_type);
    printf("设备状态：%d\n", current->device_state);
    printf("-----------------------------\n");
}



